package com.example.big_orange2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.big_orange2.authorization.annotation.AuthIgnore;
import com.example.big_orange2.authorization.manager.TokenManager;
import com.example.big_orange2.common.Result;
//import as r com.example.big_orange2.utils.Result;
import com.example.big_orange2.entity.*;
import com.example.big_orange2.mapper.StaffMapper;
import com.example.big_orange2.mapper.StafflogMapper;
import com.example.big_orange2.service.StaffService;
import com.example.big_orange2.service.WeChatService;
import com.example.big_orange2.utils.RSAutils;
import com.example.big_orange2.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
    StaffMapper staffMapper;
    @Resource
    StafflogMapper stafflogMapper;
    @Resource
    StaffService staffService;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private RedisUtils ru;

    @Autowired
    WeChatService weChatService;

    //引用公钥
    @Value("${publicKey}")
    private String publicKey;
    //引用私钥
    @Value("${privateKey}")
    private String privateKey;


    @AuthIgnore
    @GetMapping("/getrsa")//占位
    public Result<?> geteRsa(){//?是泛型，表示任意类型
        return  Result.success(publicKey);
        //返回公钥
    }

    @AuthIgnore
    @PostMapping("/login")
    public Result<?> login(@RequestBody Staff staff) {
        Result<?> result=new Result<>();
        result=staffService.login(staff);
        return  result;
    }

    @AuthIgnore
    @RequestMapping("/test")
    public Result<?> test(){
        stafflogMapper.addlog(2,1,"11","22");
        return Result.success();
    }

    //增
    //向数据库写入（insert）数据的接口
    @PostMapping("/add")
    public Result<?> save(@RequestBody Staff staff){//?是泛型，表示任意类型
        RSAutils rsautils=new RSAutils();
        staff.setPasswords(rsautils.decode(staff.getPasswords(),privateKey));
        staffMapper.insert(staff);
        return Result.success();//返回操作情况
    }

    //向数据库删除（delete）数据的接口
    @DeleteMapping("/delete/{staffid}")//占位
    public Result<?> delete(@PathVariable String staffid){//?是泛型，表示任意类型
        //删除的操作
        staffMapper.deleteById(staffid);
        return Result.success();//返回操作情况

    }
    //修改功能
    @AuthIgnore
    @PutMapping("/update")
    //向数据库更新（update）数据的接口
    public Result<?> update(@RequestBody Staff staff){//?是泛型，表示任意类型
        RSAutils rsautils=new RSAutils();
        staff.setPasswords(rsautils.decode(staff.getPasswords(),privateKey));
        staffMapper.updateById(staff);
        return Result.success();
    }

    //全部查询
    @GetMapping("/getdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> finduser(){
        LambdaQueryWrapper<Staff> wrapper = Wrappers.<Staff>lambdaQuery();
        List<Staff> Loanhandle = staffMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }

    @PostMapping("/paging/{page}/{limit}")
    //分页查询(select),查询数据接口
    public Result<?> findPage(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              @RequestBody(required = false)  String s
    ){
        Result<?> result=new Result<>();
        result=staffService.findPage(page,limit,s);
        return  result;

    }
    @GetMapping("/getdata/{staffid}")
    //根据员工号，挑选他负责的客户信息
    public Result<?> findoneuser(@PathVariable String staffid){
        LambdaQueryWrapper<Staff> wrapper =Wrappers.<Staff>lambdaQuery();
        wrapper.eq(Staff::getStaffid,staffid);//多条件查询
        List<Staff> Loanhandle = staffMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }

    @AuthIgnore
    @PostMapping("find")
    public Result findU(@RequestBody Staff staff) {
        Result<?> result=new Result<>();
        result=staffService.findU(staff);
        return  result;
    }

//////-------------------------------------------//////////////

    //通过用户的code获取openid 并且判断管理者中是否有对应该用户绑定
    @AuthIgnore
    @PostMapping("/login/wx")
    public Result<?> get3rd(@RequestBody code code){
        Result<?> result=new Result<>();
        result=staffService.get3rd(code);
        return  result;
    }


    @AuthIgnore
    @PostMapping(value = "/login/pin")
    public Result<?> logind_t(@RequestBody Staff staff) {
        Result<?> result=new Result<>();
        result=staffService.logind_t(staff);
        return  result;

    }

    @PutMapping(value = "/setStaffInfo")
    public Result<?> setStaffInfo(@RequestBody Staff staff) {
        Result<?> result = new Result<>();
        staffMapper.updateById(staff);
        result.setCode("0");
        result.setMsg("修改成功");

        return result;
    }

    @PutMapping(value = "/bindWeChat/{code}")
    public Result<?> setStaffOpenid(@RequestBody Staff staff, @PathVariable String code) {
        Result<?> result=new Result<>();
        result=staffService.setStaffOpenid(staff,code);
        return  result;
    }

}

